/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIAgregarUsuario.java
 *
 * Created on 30/10/2011, 05:24:26 PM
 */
package GUI.Seguridad;

import BE.*;
import BL.*;
import GUI.Empleados.GUIMantenimientoEmpleado;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Jorge
 */
public class GUIAgregarUsuario extends javax.swing.JDialog {
    final static String badchars = "`~!@#$%^&*()_+=\\|\"':;?/>.<, ";
    BEEmpleado objEmpleado;
    BLPerfil objGestorPerfil;
    BLUsuario objGestorUsuario;
    BLContrasenha objGestorContrasenha;
    BLEmpleado objGestorEmpleado;
    BEUsuario objUsuario;
    boolean editar;
    /** Creates new form GUIAgregarUsuario */
    public GUIAgregarUsuario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        objGestorPerfil = new BLPerfil();
        objGestorUsuario = new BLUsuario();
        objGestorContrasenha = new BLContrasenha();
        objGestorEmpleado= new BLEmpleado();
        editar=false;
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);
    }

    public GUIAgregarUsuario(java.awt.Frame parent, boolean rootPaneCheckingEnabled, BEUsuario aux) {
        super(parent, rootPaneCheckingEnabled);
        objGestorPerfil = new BLPerfil();
        objGestorUsuario = new BLUsuario();
        objGestorContrasenha = new BLContrasenha();
        objUsuario= new BEUsuario();
        objGestorEmpleado= new BLEmpleado();
        objUsuario= aux;
        editar=true;
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlDatosPersonal = new javax.swing.JPanel();
        lbIdTrabajador = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        lbIdEmpleado = new javax.swing.JLabel();
        lbNombreEmpleado = new javax.swing.JLabel();
        lbCargo = new javax.swing.JLabel();
        txtIdTrabajador = new javax.swing.JTextField();
        txtIdEmpleado = new javax.swing.JTextField();
        txtCargo = new javax.swing.JTextField();
        btnBuscarUsuario = new javax.swing.JButton();
        pnlDatosUsuario = new javax.swing.JPanel();
        lbContrasenha = new javax.swing.JLabel();
        lbUsuario = new javax.swing.JLabel();
        lbConfirmarContrasenha = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        txtContrasenha = new javax.swing.JPasswordField();
        txtConfirmarContrasenha = new javax.swing.JPasswordField();
        pnlPerfil = new javax.swing.JPanel();
        lbIdPerfil = new javax.swing.JLabel();
        lbDescripcionPerfil = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtfldDescripcion = new javax.swing.JTextArea();
        cmbPerfil = new javax.swing.JComboBox();
        btnCancelar = new javax.swing.JButton();
        btnAsignar = new javax.swing.JButton();
        pnlInterfaces = new javax.swing.JPanel();
        pnlMantenimientos = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstVDMantenimientos = new javax.swing.JList();
        pnlProcesamiento = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        lstVDProcesamiento = new javax.swing.JList();
        pnlReportes = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        lstVDReportes = new javax.swing.JList();
        pnlSeguridad = new javax.swing.JPanel();
        jScrollPane8 = new javax.swing.JScrollPane();
        lstVDSeguridad = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agregar Usuario");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        pnlDatosPersonal.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del Empleado"));

        lbIdTrabajador.setText("Id Empleado:");

        txtNombre.setEditable(false);

        lbIdEmpleado.setText("Id Empleado:");

        lbNombreEmpleado.setText("Nombre:");

        lbCargo.setText("Cargo:");

        txtIdTrabajador.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtIdTrabajadorKeyTyped(evt);
            }
        });

        txtIdEmpleado.setEditable(false);

        txtCargo.setEditable(false);

        btnBuscarUsuario.setText("Buscar...");
        btnBuscarUsuario.setMaximumSize(new java.awt.Dimension(100, 25));
        btnBuscarUsuario.setMinimumSize(new java.awt.Dimension(100, 25));
        btnBuscarUsuario.setPreferredSize(new java.awt.Dimension(100, 25));
        btnBuscarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarUsuarioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlDatosPersonalLayout = new javax.swing.GroupLayout(pnlDatosPersonal);
        pnlDatosPersonal.setLayout(pnlDatosPersonalLayout);
        pnlDatosPersonalLayout.setHorizontalGroup(
            pnlDatosPersonalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosPersonalLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosPersonalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbIdEmpleado)
                    .addComponent(lbIdTrabajador)
                    .addComponent(lbNombreEmpleado)
                    .addComponent(lbCargo))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosPersonalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtIdEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlDatosPersonalLayout.createSequentialGroup()
                        .addComponent(txtIdTrabajador, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnBuscarUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                    .addComponent(txtCargo))
                .addContainerGap(88, Short.MAX_VALUE))
        );
        pnlDatosPersonalLayout.setVerticalGroup(
            pnlDatosPersonalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosPersonalLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosPersonalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbIdTrabajador)
                    .addComponent(txtIdTrabajador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscarUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosPersonalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbIdEmpleado)
                    .addComponent(txtIdEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosPersonalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbNombreEmpleado)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosPersonalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbCargo)
                    .addComponent(txtCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlDatosUsuario.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Usuario"));

        lbContrasenha.setText("Contraseña:");

        lbUsuario.setText("Usuario:");

        lbConfirmarContrasenha.setText("Confirmar Contraseña:");

        javax.swing.GroupLayout pnlDatosUsuarioLayout = new javax.swing.GroupLayout(pnlDatosUsuario);
        pnlDatosUsuario.setLayout(pnlDatosUsuarioLayout);
        pnlDatosUsuarioLayout.setHorizontalGroup(
            pnlDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosUsuarioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbUsuario)
                    .addComponent(lbContrasenha)
                    .addComponent(lbConfirmarContrasenha))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtConfirmarContrasenha, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtContrasenha, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)))
                .addContainerGap(153, Short.MAX_VALUE))
        );
        pnlDatosUsuarioLayout.setVerticalGroup(
            pnlDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosUsuarioLayout.createSequentialGroup()
                .addGroup(pnlDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbUsuario)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbContrasenha)
                    .addComponent(txtContrasenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbConfirmarContrasenha)
                    .addComponent(txtConfirmarContrasenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlPerfil.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Perfil"));

        lbIdPerfil.setText("Id Perfil:");

        lbDescripcionPerfil.setText("Descripcion:");

        txtfldDescripcion.setColumns(20);
        txtfldDescripcion.setEditable(false);
        txtfldDescripcion.setRows(5);
        jScrollPane2.setViewportView(txtfldDescripcion);

        cmbPerfil.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbPerfilItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout pnlPerfilLayout = new javax.swing.GroupLayout(pnlPerfil);
        pnlPerfil.setLayout(pnlPerfilLayout);
        pnlPerfilLayout.setHorizontalGroup(
            pnlPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPerfilLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbDescripcionPerfil)
                    .addComponent(lbIdPerfil))
                .addGroup(pnlPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlPerfilLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(cmbPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlPerfilLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(73, Short.MAX_VALUE))
        );
        pnlPerfilLayout.setVerticalGroup(
            pnlPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPerfilLayout.createSequentialGroup()
                .addGroup(pnlPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbIdPerfil)
                    .addComponent(cmbPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbDescripcionPerfil)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE))
                .addContainerGap())
        );

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setMinimumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnAsignar.setText("Asignar");
        btnAsignar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnAsignar.setMinimumSize(new java.awt.Dimension(100, 25));
        btnAsignar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnAsignar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAsignarActionPerformed(evt);
            }
        });

        pnlInterfaces.setBorder(javax.swing.BorderFactory.createTitledBorder("Interfaces"));

        pnlMantenimientos.setBorder(javax.swing.BorderFactory.createTitledBorder("Mantenimientos"));

        lstVDMantenimientos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstVDMantenimientos.setEnabled(false);
        jScrollPane1.setViewportView(lstVDMantenimientos);

        javax.swing.GroupLayout pnlMantenimientosLayout = new javax.swing.GroupLayout(pnlMantenimientos);
        pnlMantenimientos.setLayout(pnlMantenimientosLayout);
        pnlMantenimientosLayout.setHorizontalGroup(
            pnlMantenimientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMantenimientosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlMantenimientosLayout.setVerticalGroup(
            pnlMantenimientosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMantenimientosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                .addContainerGap())
        );

        pnlProcesamiento.setBorder(javax.swing.BorderFactory.createTitledBorder("Procesamiento"));

        lstVDProcesamiento.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstVDProcesamiento.setEnabled(false);
        jScrollPane6.setViewportView(lstVDProcesamiento);

        javax.swing.GroupLayout pnlProcesamientoLayout = new javax.swing.GroupLayout(pnlProcesamiento);
        pnlProcesamiento.setLayout(pnlProcesamientoLayout);
        pnlProcesamientoLayout.setHorizontalGroup(
            pnlProcesamientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlProcesamientoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlProcesamientoLayout.setVerticalGroup(
            pnlProcesamientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlProcesamientoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                .addContainerGap())
        );

        pnlReportes.setBorder(javax.swing.BorderFactory.createTitledBorder("Reportes"));

        lstVDReportes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstVDReportes.setEnabled(false);
        jScrollPane4.setViewportView(lstVDReportes);

        javax.swing.GroupLayout pnlReportesLayout = new javax.swing.GroupLayout(pnlReportes);
        pnlReportes.setLayout(pnlReportesLayout);
        pnlReportesLayout.setHorizontalGroup(
            pnlReportesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlReportesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlReportesLayout.setVerticalGroup(
            pnlReportesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlReportesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                .addContainerGap())
        );

        pnlSeguridad.setBorder(javax.swing.BorderFactory.createTitledBorder("Seguridad"));

        lstVDSeguridad.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstVDSeguridad.setEnabled(false);
        jScrollPane8.setViewportView(lstVDSeguridad);

        javax.swing.GroupLayout pnlSeguridadLayout = new javax.swing.GroupLayout(pnlSeguridad);
        pnlSeguridad.setLayout(pnlSeguridadLayout);
        pnlSeguridadLayout.setHorizontalGroup(
            pnlSeguridadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSeguridadLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlSeguridadLayout.setVerticalGroup(
            pnlSeguridadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSeguridadLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout pnlInterfacesLayout = new javax.swing.GroupLayout(pnlInterfaces);
        pnlInterfaces.setLayout(pnlInterfacesLayout);
        pnlInterfacesLayout.setHorizontalGroup(
            pnlInterfacesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInterfacesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlInterfacesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlMantenimientos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlProcesamiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlInterfacesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlReportes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlSeguridad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        pnlInterfacesLayout.setVerticalGroup(
            pnlInterfacesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInterfacesLayout.createSequentialGroup()
                .addGroup(pnlInterfacesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnlReportes, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlMantenimientos, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlInterfacesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlProcesamiento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlSeguridad, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlPerfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(pnlDatosUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(pnlDatosPersonal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(121, 121, 121)
                        .addComponent(btnAsignar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlInterfaces, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlDatosPersonal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlDatosUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlPerfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(pnlInterfaces, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAsignar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtIdTrabajadorKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdTrabajadorKeyTyped
        char c = evt.getKeyChar();

        if((Character.isLetter(c) && !evt.isAltDown()) 
           || badchars.indexOf(c) > -1) {
            evt.consume();
            return;
        }
        if(c == '-' ) 
        evt.consume();
        else super.processKeyEvent(evt);
}//GEN-LAST:event_txtIdTrabajadorKeyTyped

private void btnBuscarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarUsuarioActionPerformed
    
    // busco el empleado usando el mantenimiento en modo busqueda
    
    
    if(txtIdTrabajador.getText().equals(""))
    {
        GUIMantenimientoEmpleado frmMantEmpleado = new GUIMantenimientoEmpleado(null, rootPaneCheckingEnabled,objEmpleado);
        frmMantEmpleado.setVisible(true);
        objEmpleado= frmMantEmpleado.obtenerEmpleado();
        frmMantEmpleado.dispose();
    }
    else
    {
        BLEmpleado objGestorEmpleado = new BLEmpleado();
        objEmpleado= objGestorEmpleado.llenaVentana(Integer.parseInt(txtIdTrabajador.getText()));
    }
    
    
    //lleno la ventana
    
    if (objEmpleado!=null)
    {
        txtIdEmpleado.setText(objEmpleado.getIdPersona()+"");
        txtNombre.setText(objEmpleado.getApePaterno()+" "+objEmpleado.getApeMaterno()+", "+objEmpleado.getNombre());
        txtCargo.setText(objEmpleado.getCargo());
    }
    else
    {
        txtIdEmpleado.setText("");
        txtNombre.setText("");
        txtCargo.setText("");
    }
    
   
}//GEN-LAST:event_btnBuscarUsuarioActionPerformed

private void cmbPerfilItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbPerfilItemStateChanged
    BEPerfil aux=(BEPerfil) cmbPerfil.getSelectedItem();
    txtfldDescripcion.setText(aux.getDescripcion());
    objGestorPerfil.cargarLista(lstVDMantenimientos, aux.getInterfacesMantenimiento());
    objGestorPerfil.cargarLista(lstVDProcesamiento,aux.getInterfacesProcesamiento());
    objGestorPerfil.cargarLista(lstVDReportes, aux.getInterfacesReporte());
    objGestorPerfil.cargarLista(lstVDSeguridad, aux.getInterfacesSeguridad());
}//GEN-LAST:event_cmbPerfilItemStateChanged

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    
    objGestorPerfil.cargar();
    
    for (BEPerfil p:objGestorPerfil.getLstPerfiles()) if(p.getEstado().equals("ACTIVO")) cmbPerfil.addItem(p);
    BEPerfil aux=(BEPerfil) cmbPerfil.getSelectedItem();
    txtfldDescripcion.setText(aux.getDescripcion());
    objGestorPerfil.cargarLista(lstVDMantenimientos, aux.getInterfacesMantenimiento());
    objGestorPerfil.cargarLista(lstVDProcesamiento,aux.getInterfacesProcesamiento());
    objGestorPerfil.cargarLista(lstVDReportes, aux.getInterfacesReporte());
    objGestorPerfil.cargarLista(lstVDSeguridad, aux.getInterfacesSeguridad());
    
    if (editar)
       
    {
        modoEdicion();
        //cargo los datos del usuario
        
        BLEmpleado objGestorEmpleado = new BLEmpleado();
        objEmpleado= objGestorEmpleado.obtenerEmpleadoPorIdUsuario(objUsuario.getIdUsuario());
        
        txtIdTrabajador.setText(objEmpleado.getIdEmpleado()+"");
        txtIdEmpleado.setText(objEmpleado.getIdEmpleado()+"");
        
        txtNombre.setText(objEmpleado.getApePaterno()+" "+objEmpleado.getApeMaterno()+", "+objEmpleado.getNombre());
        txtCargo.setText(objEmpleado.getCargo());
        
        
        //cargo los datos de la contrasenha
        objGestorContrasenha.cargar();
        BEContrasenha objContrsenha=objGestorContrasenha.obtenerContrasenhaActualIdUsuario(objUsuario.getIdUsuario());
        
        txtUsuario.setText(objUsuario.getUsuario());
        
        txtContrasenha.setText(objContrsenha.getContrasenha());
        txtConfirmarContrasenha.setText(objContrsenha.getContrasenha());
        
        //cargo los datos del perfil
        for (int i=0;i<cmbPerfil.getItemCount();i++)
        {
            if(((BEPerfil)cmbPerfil.getItemAt(i)).getIdPerfil()==objUsuario.getIdPerfil()) cmbPerfil.setSelectedIndex(i);
        }
        
        aux=(BEPerfil) cmbPerfil.getSelectedItem();
        txtfldDescripcion.setText(aux.getDescripcion());
        objGestorPerfil.cargarLista(lstVDMantenimientos, aux.getInterfacesMantenimiento());
        objGestorPerfil.cargarLista(lstVDProcesamiento,aux.getInterfacesProcesamiento());
        objGestorPerfil.cargarLista(lstVDReportes, aux.getInterfacesReporte());
        objGestorPerfil.cargarLista(lstVDSeguridad, aux.getInterfacesSeguridad());
        //txtContrasenha.setText(objUsuario);
    }
}//GEN-LAST:event_formWindowOpened

private void btnAsignarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAsignarActionPerformed

    int result;
    
    if(editar) 
    {
        result=JOptionPane.showConfirmDialog(null,"Desea actualizar los datos del usuario?", "Edicion de Usuario",    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    }
    else
    {
        result=JOptionPane.showConfirmDialog(null,"Desea registrar este usuario?", "Registro de Usuario",    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    }
    
            //JOptionPane.showMessageDialog(null,"Problema al consultar la base de datos"+e.toString(),"Error",JOptionPane.ERROR_MESSAGE);

    if (result==JOptionPane.YES_OPTION)
    {
    
    if(!txtIdEmpleado.getText().equals(""))
    {
        //creo el usuario
        //JOptionPane.showMessageDialog(null,(txtContrasenha.getText()),"Registro de Usuario",JOptionPane.ERROR_MESSAGE);
        //JOptionPane.showMessageDialog(null,(txtConfirmarContrasenha.getText()),"Registro de Usuario",JOptionPane.ERROR_MESSAGE);
        if ((txtContrasenha.getText()).equals(txtConfirmarContrasenha.getText()))
        {
            if((!(objGestorUsuario.existeUsuario(txtUsuario.getText()))))
            {
                if (objGestorContrasenha.validarContrasenha(txtContrasenha.getText()))
                {
                    if (editar)
                    {
                        //actualizo el usuario
                        objUsuario.setUsuario(txtUsuario.getText());
                        objUsuario.setIdPerfil(((BEPerfil)cmbPerfil.getSelectedItem()).getIdPerfil());
                        objUsuario.setEstado("ACTIVO");
                        
                        //creo la contrasenha
                        BEContrasenha objContrasenha = new BEContrasenha();
                        objContrasenha=null;
                        //objContrasenha.setIdUsuario(objUsuario.getIdUsuario());
                        //objContrasenha.setFechaCreacion(new Date());
                        //objContrasenha.setContrasenha(txtContrasenha.getText());
                        //objContrasenha.setEstado("ACTIVO");
                        
                        String guardar = objGestorUsuario.actualizar(objEmpleado,(BEPerfil)cmbPerfil.getSelectedItem(),objUsuario,objContrasenha);

                        JOptionPane.showMessageDialog(null,guardar,"Registro de Perfil",JOptionPane.INFORMATION_MESSAGE);

                        this.dispose(); 
                        
                    }
                    else
                    {
                        
                        if (!objGestorEmpleado.tieneUsuarioAsignado(objEmpleado))
                        {
                            //creo el usuario
                            objUsuario= new BEUsuario();
                            objUsuario.setEstado("ACTIVO");
                            Date fecha = new Date();
                            objUsuario.setFechaCreacion(fecha);
                            objUsuario.setIdPerfil(((BEPerfil)cmbPerfil.getSelectedItem()).getIdPerfil());
                            objUsuario.setUsuario(txtUsuario.getText());

                            //creo la contrasenha

                            BEContrasenha objContrasenha = new BEContrasenha();
                            objContrasenha.setFechaCreacion(fecha);
                            objContrasenha.setContrasenha(txtContrasenha.getText());
                            objContrasenha.setEstado("ACTIVO");

                            //System.out.println("El idPerfil es: "+((BEPerfil)cmbPerfil.getSelectedItem()).getIdPerfil());

                            String guardar = objGestorUsuario.guardar(objEmpleado,(BEPerfil)cmbPerfil.getSelectedItem(),objUsuario,objContrasenha);

                            JOptionPane.showMessageDialog(null,guardar,"Registro de Perfil",JOptionPane.INFORMATION_MESSAGE);

                            this.dispose(); 
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null,"El Empleado ya tiene un usuario asignado.","Registro de Perfil",JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                      
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"Ingrese una contraseña valida.","Registro de Usuario",JOptionPane.ERROR_MESSAGE);
                }
            }
            else
            {
                JOptionPane.showMessageDialog(null,"El usuario ya existe, escriba otro usuario.","Registro de Usuario",JOptionPane.ERROR_MESSAGE);
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Las contraseñas no son iguales, reingrese las contrasenãs.","Registro de Usuario",JOptionPane.ERROR_MESSAGE);
        }
              
    }
    else
    {
        JOptionPane.showMessageDialog(null,"Seleccione un trabajador para asignar un usuario.","Registro de Usuario",JOptionPane.ERROR_MESSAGE);
    }
    
    }
   
    
}//GEN-LAST:event_btnAsignarActionPerformed

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIAgregarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIAgregarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIAgregarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIAgregarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                GUIAgregarUsuario dialog = new GUIAgregarUsuario(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAsignar;
    private javax.swing.JButton btnBuscarUsuario;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cmbPerfil;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JLabel lbCargo;
    private javax.swing.JLabel lbConfirmarContrasenha;
    private javax.swing.JLabel lbContrasenha;
    private javax.swing.JLabel lbDescripcionPerfil;
    private javax.swing.JLabel lbIdEmpleado;
    private javax.swing.JLabel lbIdPerfil;
    private javax.swing.JLabel lbIdTrabajador;
    private javax.swing.JLabel lbNombreEmpleado;
    private javax.swing.JLabel lbUsuario;
    public javax.swing.JList lstVDMantenimientos;
    private javax.swing.JList lstVDProcesamiento;
    private javax.swing.JList lstVDReportes;
    private javax.swing.JList lstVDSeguridad;
    private javax.swing.JPanel pnlDatosPersonal;
    private javax.swing.JPanel pnlDatosUsuario;
    private javax.swing.JPanel pnlInterfaces;
    private javax.swing.JPanel pnlMantenimientos;
    private javax.swing.JPanel pnlPerfil;
    private javax.swing.JPanel pnlProcesamiento;
    private javax.swing.JPanel pnlReportes;
    private javax.swing.JPanel pnlSeguridad;
    private javax.swing.JTextField txtCargo;
    private javax.swing.JPasswordField txtConfirmarContrasenha;
    private javax.swing.JPasswordField txtContrasenha;
    private javax.swing.JTextField txtIdEmpleado;
    private javax.swing.JTextField txtIdTrabajador;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtUsuario;
    private javax.swing.JTextArea txtfldDescripcion;
    // End of variables declaration//GEN-END:variables

    private void modoEdicion() {
        this.setTitle("Editar Usuario: "+objUsuario.getUsuario());
        this.txtIdEmpleado.setEditable(false);
        this.txtIdTrabajador.setEditable(false);
        this.btnBuscarUsuario.setVisible(false);
        this.txtContrasenha.setEditable(false);
        this.txtConfirmarContrasenha.setEditable(false);
    }
}
